package com.msb.mall.product.service.impl;

import com.msb.mall.product.service.CategoryBrandRelationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;

import com.msb.mall.product.dao.CategoryDao;
import com.msb.mall.product.entity.CategoryEntity;
import com.msb.mall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

//    @Autowired
//    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询所有的类别数据，然后将数据封装为树形结构，便于前端使用
     *
     * @param params
     * @return
     */
    @Override
    public List<CategoryEntity> queryPageWithTree(Map<String, Object> params) {
        // 1.查询所有的商品分类信息
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        // 2.将商品分类信息拆解为树形结构【父子关系】
        // 2.1遍历出所有的大类 parent_cid = 0
        List<CategoryEntity> list = categoryEntities.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0)
                .map(categoryEntity -> {
                    // 根据大类找到所有的小类，递归的方式实现
                    categoryEntity.setChildrenCategoryEntityList(getCategoryChildrenList(categoryEntity, categoryEntities));
                    return categoryEntity;
                }).sorted(Comparator.comparingInt(entity -> (entity.getSort() == null ? 0 : entity.getSort()))).collect(Collectors.toList());
        // 2.2根据大类找到对应的所有的小类
        return list;
    }

    private List<CategoryEntity> getCategoryChildrenList(CategoryEntity categoryEntity, List<CategoryEntity> categoryEntities) {
        List<CategoryEntity> collect = categoryEntities.stream().filter(entity -> {
            return entity.getParentCid().equals(categoryEntity.getCatId());
        }).map(entity -> {
            entity.setChildrenCategoryEntityList(getCategoryChildrenList(entity, categoryEntities));
            return entity;
        }).sorted(Comparator.comparingInt(entity -> (entity.getSort() == null ? 0 : entity.getSort()))).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void removeCategoriesByIds(List<Long> ids) {
        // TODO  1.检查类别数据是否在其他业务中使用

        // 2.批量逻辑删除操作
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public Long[] findCateLogPaths(Long cateLogId) {
        List<Long> cateLogPaths = new ArrayList<>();
        cateLogPaths = findParentCatePath(cateLogId, cateLogPaths);
        Collections.reverse(cateLogPaths);
        return cateLogPaths.toArray(new Long[cateLogPaths.size()]);
    }

    private List<Long> findParentCatePath(Long cateLogId, List<Long> cateLogPaths) {
        cateLogPaths.add(cateLogId);
        CategoryEntity categoryEntity = this.getById(cateLogId);
        if (categoryEntity.getParentCid() != 0) {
            findParentCatePath(categoryEntity.getParentCid(), cateLogPaths);
        }
        return cateLogPaths;
    }

    @Override
    public void updateDetail(CategoryEntity categoryEntity) {
//        this.updateById(categoryEntity);
//        if (StringUtils.isNotEmpty(categoryEntity.getName())) {
//            categoryBrandRelationService.updateCatelogName(
//                    categoryEntity.getCatId(),categoryEntity.getName());
//        }
    }
}